﻿using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;
using LTJ.FrameWork;

namespace LTJ.WebPage.SystemManage.basedata
{
    /// <summary>
    /// 整柜基础数据管理
    /// </summary>
    public partial class fclbasedatagrid : AdminPage
    {
        /// <summary>
        /// 基础数据标识
        /// </summary>
        protected string typecode = SASRequest.GetString("tcode");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataGrid();
            }
        }

        protected void BindDataGrid()
        {
            string strWhere = "code = '" + typecode + "'";

            if (ViewState["condition"] == null || ViewState["condition"].ToString() == "")
            {
                strWhere += "";
            }
            else
            {
                strWhere += " AND " + ViewState["condition"].ToString();
            }

            DataGrid1.VirtualItemCount = BaseDatas.GetBaseDataCount(strWhere);
            DataGrid1.DataSource = BaseDatas.GetBaseDataByPage(DataGrid1.CurrentPageIndex + 1, DataGrid1.PageSize, strWhere, "");
            DataGrid1.DataBind();
        }

        protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            BindDataGrid();
        }

        protected void GoToPagerButton_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 搜索点击
        /// </summary>
        protected void check_Click(object sender, EventArgs e)
        {
            string conditionstr = SASRequest.GetString("corpname");
            if (conditionstr != "" && Utils.IsSafeSqlString(conditionstr))
            {
                ViewState["condition"] = "lname like '%" + conditionstr + "%'";
            }
            else
            {
                ViewState["condition"] = "";
            }
            DataGrid1.CurrentPageIndex = 0;
            BindDataGrid();
        }

        /// <summary>
        /// 重新绑定
        /// </summary>
        protected void reclick_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 删除规则
        /// </summary>
        protected void del_Click(object sender, EventArgs e)
        {
            if (SASRequest.GetString("cid") != "")
            {
                BaseDatas.DelBaseData(SASRequest.GetString("cid"));
                //Routes.DelRoutes(SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        /// <summary>
        /// 数据变更提交
        /// </summary>
        protected void datapost_Click(object sender, EventArgs e)
        {
            int dataid = SASRequest.GetInt("dataid", 0);
            string data_code = _code.Text.Trim();
            string data_name = _name.Text.Trim();
            string data_ename = _ename.Text.Trim();

            if (string.IsNullOrEmpty(data_code) || string.IsNullOrEmpty(data_name) || string.IsNullOrEmpty(data_ename))
            {
                base.RegisterStartupScript("PAGETEMPLATE", "请输入完整的数据信息！");
                return;
            }

            BaseDataInfo binfo = new BaseDataInfo();
            binfo.code = typecode;
            binfo.tcode = data_code;
            binfo.name = data_name;
            binfo.ename = data_ename;

            if (dataid > 0)
            {
                binfo.id = dataid;
                BaseDatas.UpdateBaseData(binfo);
            }
            else
            {
                if (BaseDatas.CreateBaseData(binfo) == -1)
                {
                    base.RegisterStartupScript("PAGETEMPLATE", "数据信息已存在！");
                    return;
                }
            }

            BindDataGrid();
        }

        #region Web 窗体设计器生成的代码

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            string headstr = "基础数据列表";

            switch (typecode)
            {
                case "carrier":
                    headstr = "船公司数据列表";
                    break;
                case "country":
                    headstr = "国家和地区数据列表";
                    break;
                case "dock":
                    headstr = "码头数据列表";
                    break;
                case "searoute":
                    headstr = "航线数据列表";
                    break;
                case "ports":
                    headstr = "港口数据列表";
                    DataGrid1.Columns[DataGrid1.Columns.Count - 3].Visible = true;
                    DataGrid1.Columns[DataGrid1.Columns.Count - 2].Visible = true;
                    DataGrid1.Columns[DataGrid1.Columns.Count - 1].Visible = true;
                    break;
            }

            DataGrid1.PageIndexChanged += new DataGridPageChangedEventHandler(DataGrid1_PageIndexChanged);
            DataGrid1.GoToPagerButton.Click += new EventHandler(GoToPagerButton_Click);
            DataGrid1.AllowCustomPaging = true;
            DataGrid1.TableHeaderName = headstr;
            DataGrid1.Attributes.Add("borderStyle", "0");
            DataGrid1.DataKeyField = "id";
            DataGrid1.ColumnSpan = 12;
            DataGrid1.SaveDSViewState = true;

            //check.Click += new EventHandler(check_Click);
            reclick.ServerClick += new EventHandler(reclick_Click);
            del.Click += new EventHandler(del_Click);
            datapost.Click += new EventHandler(datapost_Click);
        }

        #endregion
    }
}